package cn.niit.controller;


import cn.niit.entity.Classes;
import cn.niit.entity.Course;
import cn.niit.entity.Score;
import cn.niit.entity.Student;
import cn.niit.entity.query.ScoreQuery;
import cn.niit.service.ClassService;
import cn.niit.service.CourseService;
import cn.niit.service.ScoreService;
import cn.niit.service.StudentService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@Controller
@RequestMapping("/score")
public class ScoreController {
    @Resource
    private ScoreService scoreService;
    @Resource
    private StudentService studentService;
    @Resource
    private ClassService classService;
    @Resource
    private CourseService courseService;

    @GetMapping("/queryAll")
    public String queryAll(Model model, ScoreQuery scoreQuery) {
        log.info("ScoreController.queryAll 查询成绩列表 进入控制器。。。。。。。。。。。。。。。");
        PageInfo<Score> scorePageInfo = scoreService.queryAll(scoreQuery);
        List<Classes> classesList=classService.queryAllClass();
        List<Course> courseList=courseService.queryAllCourseList();

        model.addAttribute("classesList",classesList);
        model.addAttribute("courseList",courseList);
        model.addAttribute("page", scorePageInfo);
        log.info("ScoreController.queryAll 查询成绩列表 进入控制器。。。。。。。。。。。。。。。");
        return "/score/score";
    }

    @GetMapping("/select")
    public String select(Model model, ScoreQuery scoreQuery) {
        PageInfo<Score> scorePageInfo = scoreService.select(scoreQuery);
        List<Classes> classesList=classService.queryAllClass();
        List<Course> courseList=courseService.queryAllCourseList();

        model.addAttribute("classesList",classesList);
        model.addAttribute("courseList",courseList);
        model.addAttribute("page", scorePageInfo);
        return "/score/score";
    }


    @GetMapping("/edit/{id}")
    public String toEdit(@PathVariable("id") Integer id, Model model) {
        List<Classes> classesList=classService.queryAllClass();
        List<Course> courseList=courseService.queryAllCourseList();

        model.addAttribute("classesList",classesList);
        model.addAttribute("courseList",courseList);
        model.addAttribute("score", scoreService.queryScoreById(id));
        return "/score/editScore";
    }

    @PostMapping("/edit")
    public String edit(Score score) {
        ScoreQuery scoreQuery = new ScoreQuery();
        Integer id = score.getId();
        scoreQuery.setStudentCode(score.getStudentCode());
        PageInfo<Score> scorePageInfo = scoreService.queryByStudentCode(scoreQuery);
        boolean b;
        if (id != null) {
            b = scoreService.editScore(score);
        } else {
            b = scoreService.addScore(score);
        }
        return "redirect:/score/queryAll";
    }

    @GetMapping("/update")
    public String update(Model model) {
        List<Classes> classesList=classService.queryAllClass();
        List<Course> courseList=courseService.queryAllCourseList();
        model.addAttribute("classesList",classesList);
        model.addAttribute("courseList",courseList);
        Score score = new Score();
        model.addAttribute("score", score);
        return "/score/editScore";
    }

    @GetMapping("/delete/{id}")
    public String deleteScoreById(@PathVariable("id") Integer id) {
        boolean b = scoreService.deleteScoreById(id);
        return "redirect:/score/queryAll";
    }
}